package gui;
	
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;

import conference.Author;
import conference.Conference;
import conference.Manuscript;
import conference.Review;
import conference.Reviewer;
import conference.User;
import java.awt.event.*;
import java.awt.*;
import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;

public class ReviewerSubmitPanel extends Observable {
	private static final String TITLE = "Submit a Review";
	JFileChooser fileChooser;
	private JPanel myPanel;
	public static int BORDER_SPACE = 20;
	private Conference myConference;
	private User myUser;
	private File myFile;
	
	/**
	 * Creates a Author submit Manuscript Panel
	 */
    public ReviewerSubmitPanel(final Conference theConference, final Reviewer theUser) {
    	
    	myPanel = new JPanel(getBorderLayout());
		myConference = theConference;
    	myUser = theUser;
    	
    	//Display the window.
    	setUpContents();
    	myPanel.setOpaque(true);
    }
	
    /**
     * Returns the panel for this object.
     * @return This panel as JPanel.
     */
    public JPanel getPanel() {
    	JPanel contentPanel = new JPanel(new BorderLayout());
    	JPanel quitSavePanel = new JPanel(getBorderLayout());
    	
    	JPanel westPanel = new JPanel();
    	JLabel blankLabel = new JLabel("     ");
    	westPanel.add(GuiController.getSwitchConferenceButton());
    	westPanel.add(GuiController.getSwitchUserButton());
    	westPanel.add(GuiController.getQuitButton());
    	westPanel.add(GuiController.getSaveButton());
    	westPanel.add(blankLabel, BorderLayout.EAST);
    	quitSavePanel.add(westPanel, BorderLayout.EAST);
    	
    	//add Panels to contentPanel
    	contentPanel.add(myPanel, BorderLayout.CENTER);
    	contentPanel.add(quitSavePanel, BorderLayout.SOUTH);
    	
    	return contentPanel;    	
    }
    
    /**
     * Creates and sets up components of JPanel.
     */
    private void setUpContents() { 
    	JPanel northPanel = new JPanel(getBorderLayout());
    	JPanel centerPanel = new JPanel(getGridLayout());
    	JPanel southPanel = new JPanel(new FlowLayout());
    	JPanel eastPanel = new JPanel(new FlowLayout());
    	JPanel westPanel = new JPanel(new FlowLayout());
    	
    	//setup eastPanel
    	JLabel blankPanel = new JLabel("      ");
    	eastPanel.add(blankPanel);
    	
    	//setup westPanel
    	westPanel.add(blankPanel);
    	
    	//setup northPanel
    	JLabel previousManLabel = new JLabel("Manuscripts to review: ");
    	northPanel.add(blankPanel, BorderLayout.WEST);
    	northPanel.add(previousManLabel, BorderLayout.CENTER);
    	
    	
    	//setup centerPanel
    	Manuscript[] myMans = myConference.getManuscriptByReviewer((Reviewer)myUser);
    	for (int i = 0; i < myMans.length; i++) {
    		System.out.println("My Author is: " + myMans[i].getAuthor().getLastCommaFirst());
    		for (Review r : myMans[i].getReviews()) {
    			System.out.println("Reviewer: " + r.getReviewer().getLastCommaFirst() + " - " + myMans[i].getTitle());
    			if (r.getReviewer().getLastCommaFirst().equals(myUser.getLastCommaFirst())) {
    				//TODO - Need to figure out problem with Reviewer.equals
    				centerPanel.add(getNewButton(myMans[i], r));
    			}
    		}
    	}
    	
    	//setup southPanel
    	southPanel.add(blankPanel);
    	
        //setup myPanel
        myPanel.add(northPanel, BorderLayout.NORTH);
        myPanel.add(southPanel, BorderLayout.SOUTH);
        myPanel.add(centerPanel, BorderLayout.CENTER);
        myPanel.add(eastPanel, BorderLayout.EAST);
        myPanel.add(westPanel, BorderLayout.WEST);
    }
    
    /**
     * Creates a new button for manuscripts.
     * @param title The title of the manuscript.
     * @return A new button as JButton.
     */
    private JButton getNewButton(final Manuscript theManuscript, final Review theReview) {
		JButton button = new JButton("Author: " + theManuscript.getAuthor().getLastCommaFirst() + "     Title: " + theManuscript.getTitle() + "     Status: " + (theReview.isComplete()?"Reviewed" : "Not Reviewed" ));
		button.addActionListener(
				new ActionListener() {
					public void actionPerformed(final ActionEvent theEvent) {
						final JOptionPane reviewQuestionPane = new JOptionPane();
						int result = -1;
						if (theReview.isComplete()){
							//ask if remove review
							result = reviewQuestionPane.showConfirmDialog(null, "Would you like to remove your review for this manuscript?", "Remove Review?", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
							if (result == 0) {
								//remove review for this reviewer from manuscript.
								Review newReview = new Review(theReview.getReviewer());
								myConference.submitNewReview(theManuscript, theReview, newReview);
								setChanged();
								notifyObservers(State.REVIEW_SUBMITS);
							}
						}
						else {
							//ask if submit review
							result = reviewQuestionPane.showConfirmDialog(null, "Would you like to submit a review for this manuscript?", "Submit Review?", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
							if (result == 0) {
								//get File location for review.
								FileNameExtensionFilter filter = new FileNameExtensionFilter("txt & text", "txt", "text");
		        				JFileChooser fileChooser = new JFileChooser();
		        				fileChooser.setFileFilter(filter);
		        				
		        				int returnVal = fileChooser.showOpenDialog(null);
		        						        				
		        				if(returnVal == JFileChooser.APPROVE_OPTION) {
		        					myFile = new File(fileChooser.getSelectedFile().getAbsolutePath());
		        				
		        					//add review into manuscript
		        					Review newReview = new Review("", myFile, theReview.getReviewer());
		        					myConference.submitNewReview(theManuscript, theReview, newReview);
		        					setChanged();
		        					notifyObservers(State.REVIEW_SUBMITS);
		        				}								
							}
						}
					}
				}
		);
		return button;
    }

	/**
     * Sets up the border layout.
     * @return BorderLayout object.
     */
    private static BorderLayout getBorderLayout() {
    	BorderLayout borderLayout = new BorderLayout();
    	borderLayout.setHgap(BORDER_SPACE);
    	return borderLayout;
    }
    
    private static GridLayout getGridLayout() {
    	GridLayout myGridLayout = new GridLayout(0,1);
    	myGridLayout.setHgap(BORDER_SPACE * 2);
    	return myGridLayout;
    }
 

    public void main(String...args0) {
    	JFrame myFrame = new JFrame();
    }
    
	public String getTitle() {
		return TITLE;
	}
}